<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of limits
 *
 * @author b4by
 */
class limit extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_limit');
	}

	public function read() {

		$pagination = pagination_init($this->model_limit->get_total(), request('p', 1));
		$limits = $this->model_limit->get_list(array(
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('likes', 'asc')
				));

		echo json_encode(array(
			'state' => 'success',
			'data' => $limits,
			'pagination' => $pagination
		));
	}

	public function detail() {
		$id = request('id');
		$limit = $this->model_limit->get_info($id);
		if ($limit) {
			echo json_encode(array(
				'state' => 'success',
				'data' => $limit
			));
		} else {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('item_not_found'),
			));
		}
	}

	public function update() {
		$id = request('id');
		$data = request_filter(array('likes', 'images_max', 'videos_max'));

		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 'lang:limit', 'required|callback__check_id');
		$this->form_validation->set_rules('likes', 'lang:likes', 'required|is_natural');
		$this->form_validation->set_rules('images_max', 'lang:images_max', 'required|is_natural');
		$this->form_validation->set_rules('videos_max', 'lang:videos_max', 'required|is_natural');
		if ($this->form_validation->run()){ 
			$this->model_limit->update($id, $data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('update_success')
			));
		} else {
			
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data),
			));
		}
	}


	public function create() {
//		$id = request('id');
		$data = request_filter(array('likes', 'images_max', 'videos_max'));
//		pr($data);die;
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('likes', 'lang:likes', 'required|is_natural');
		$this->form_validation->set_rules('images_max', 'lang:images_max', 'required|is_natural');
		$this->form_validation->set_rules('videos_max', 'lang:videos_max', 'required|is_natural');
		if ($this->form_validation->run()){ 
			$this->model_limit->create($data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('create_success'),
			));
		} else {
			
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> lang('item_not_exist'),
			));
		}
	}

	public function delete() {
		$id = request('id');
		if ($this->model_limit->get_info($id)!==FALSE){ 
			$this->model_limit->del($id);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('delete_success'),
				'id'	=> $id,
			));
		} else {
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data),
			));
		}
	}
	public function _check_id($id)
	{
//		echo 'aaaaaaaa';
		if($this->model_limit->get_info($id)!==FALSE) 
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__,lang('item_not_exsit'));
		return FALSE;
		
	}

}

?>
